﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using AMStudio.Windows.Forms;
using AMStudio.Common;

namespace AMStudio.UserInterface
{
    public class StationUI : AMStudio.Common.AWinFormModule
    {

        protected int StationIndex = 0;

        public override bool ActivateUI()
        {
            if (!this.initialized)
            {
                this.SetUserPrivilige(null, null);
                EventBroker.RegisterSubscriber("SET_USER_PRIVILIGE", new EventHandler(SetUserPrivilige));
            }
            bool result = base.ActivateUI();
            int index = 1;
            index = int.Parse(this.UIName.Substring((this.UIName.Length - 2), 2));
            SendMessage("UI_ID", index);
            RefreshProductInformation();
            return result;
        }

        protected virtual void SetUserPrivilige(object sender, EventArgs e)
        {
            Language_InitializeControl(this);          
        }

        private void RefreshProductInformation()
        {
            try
            {
                if (WorkorderDB.ActiveProduct != null)
                {
                    Control[] findCtl = this.Controls.Find("txtProductName", true);
                    if ((findCtl != null) && (findCtl.Length > 0))
                    {
                        TextBox text = findCtl[0] as TextBox;
                        text.Text = WorkorderDB.ActiveProduct.Name;
                    }
                }
                else
                {
                    Control[] findCtl = this.Controls.Find("txtProductName", true);
                    if ((findCtl != null) && (findCtl.Length > 0))
                    {
                        TextBox text = findCtl[0] as TextBox;
                        text.Text = string.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
        }

        protected override void BackGroud_EventHandler(object sender, DelegateDispather.StationEventArgs e)
        {
            base.BackGroud_EventHandler(sender, e);

            if (e.From == "MANAGER")
            {
                switch (e.Command)
                {
                    case "STATION_RELATED":
                        {
                            string[] a_sns = (string[])e.Value;
                            Control[] findCtl = this.Controls.Find("txtHousingSN", true);
                            if ((findCtl != null) && (findCtl.Length > 0))
                            {
                                TextBox text = findCtl[0] as TextBox;
                                text.Text = a_sns[0];
                            }

                            findCtl = this.Controls.Find("txtConnectorSN", true);
                            if ((findCtl != null) && (findCtl.Length > 0))
                            {
                                TextBox text = findCtl[0] as TextBox;
                                text.Text = a_sns[1];
                            }
                        }
                        break;
                }
            }
        }
        protected override void UIDB_RfreshTag(Common.UserInterfaceItem item)
        {
            try
            {

                if (item.RelatedControl == null)
                    return;
                if (item.RelatedControl.Name == "DisplayResult")
                {
                    int productResult = (int)item.value;
                    if (productResult == 0)
                    {
                        item.RelatedControl.ForeColor = System.Drawing.Color.DarkGray;
                    }
                    else if (productResult == 1)
                    {
                        item.RelatedControl.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (productResult == 2)
                    {
                        item.RelatedControl.ForeColor = System.Drawing.Color.Red;
                    }
                    return;
                }
                if (item.RelatedControl is SimpleEllipse)
                {
                    if ((bool)item.value)
                    {
                        item.RelatedControl.ForeColor = System.Drawing.Color.Green;
                    }
                    else
                    {
                        item.RelatedControl.ForeColor = System.Drawing.Color.DarkGray;
                    }
                }
                else if (item.RelatedControl is DevComponents.DotNetBar.ButtonX)
                {
                    DevComponents.DotNetBar.ButtonX button = item.RelatedControl as DevComponents.DotNetBar.ButtonX;
                    if ((bool)item.value)
                    {
                        //button.Enabled = true;
                        button.Style = DevComponents.DotNetBar.eDotNetBarStyle.Office2003;
                        button.Checked = true;
                    }
                    else
                    {
                        //button.Enabled = false;
                        button.Style = DevComponents.DotNetBar.eDotNetBarStyle.Office2007;
                        button.Checked = false;
                    }
                }
                else if (item.RelatedControl is ComboBox)
                {
                    int productResult = (int)item.value;

                    ComboBox box = item.RelatedControl as ComboBox;

                    box.SelectedIndex = productResult;
                }
                else if (item.RelatedControl is NumericUpDown)
                {
                    int productResult=0;
                    if (item.value.ToString().IndexOf(";") >= 0)
                    {
                        string[] words = item.value.ToString().Split(new string[] { ";" }, StringSplitOptions.None);

                        if (words.Length>1)
                        {
                            UInt16 low_w = UInt16.Parse(words[0]);
                            UInt16 high_w = UInt16.Parse(words[1]);

                            UInt32 tt = (UInt32)high_w;
                            tt = (tt << 16) + low_w;
                            productResult = (Int32)tt;
                        }
                    }
                    else
                    {
                        productResult = (int)item.value;
                        
                    }
                    NumericUpDown box = item.RelatedControl as NumericUpDown;

                    box.Value = productResult;
                }
            }
            catch (Exception ex)
            {
            }
        }

        private static int LastValueChangedTickCount = Environment.TickCount;
        protected override void UIDB_ConnectTagAction(Common.UserInterfaceItem item)
        {
            //item.RelatedControl.Click += (sender, e) =>
            //{
            //    DevComponents.DotNetBar.ButtonX button = item.RelatedControl as DevComponents.DotNetBar.ButtonX;
            //    //if (button.Checked)
            //    //{
            //    SendMessage("UIITEM_ACTION", item);
            //    //}
            //};

            DevComponents.DotNetBar.ButtonX button = item.RelatedControl as DevComponents.DotNetBar.ButtonX;

            if (button != null)
            {
                item.RelatedControl.MouseDown += (sender, e) =>
                {

                    if (e.Button == System.Windows.Forms.MouseButtons.Left)
                    {
                        SendMessage("UIITEM_ACTION_MOUSE_DOWN", item);
                    }
                };

                item.RelatedControl.MouseUp += (sender, e) =>
                {
                    if (e.Button == System.Windows.Forms.MouseButtons.Left)
                    {
                        
                        SendMessage("UIITEM_ACTION_MOUSE_UP", item);
                    }
                };
            }
            else
            {

                item.RelatedControl.Click += (sender, e) =>
                {

                    //if (button.Checked)
                    //{
                    SendMessage("UIITEM_ACTION", item);
                    //}
                };
            }

                                   
            NumericUpDown nudCtrl = item.RelatedControl as NumericUpDown;
            if (nudCtrl != null)
            {
                nudCtrl.ValueChanged += (sender, e) =>
                    {
                        int CurrentTickCount = Environment.TickCount;
                        int TimeTaken = CurrentTickCount < LastValueChangedTickCount ? Int32.MaxValue - LastValueChangedTickCount + CurrentTickCount : CurrentTickCount - LastValueChangedTickCount;
                        if (TimeTaken > 300 )
                        {
                            SendMessage("UIITEM_VALUE", new object[] { item, nudCtrl.Value });
                        }

                        LastValueChangedTickCount = CurrentTickCount;
                    };
            }
        }
    }
}
